var myFunc = require("./myCommonFunction.js");
var useWiFi = true;
var closeIntenet = false;
var now = new Date();
var hour = now.getHours();

var storage = myFunc.initDateStorage();

//设置屏幕的宽度，像素值
var screen_width = device.width;
var widthParam = screen_width / 1440;
//设置屏幕的高度，像素值
var screen_height = device.height
var heightParam = screen_height / 3120;

threads.start(function () {
    sleep(360000)
    exit();
});
opreate();
// renwu();
// openWeixinJingDou()
exit();
function opreate() {
    // initParam();
    var ret = myFunc.initNetWork(null, true);
    if (ret) {
        // var shengying = device.getMusicVolume();
        myFunc.setMusicVolume(0);
        jingdong();
        myFunc.setMusicVolume(50);
    }
}

function initParam() {

    //晚上8点到早上10点用WiFi
    if (9 < hour && hour < 20) {
        useWiFi = false;
    }
    if (hour < 9 || hour == 24) {
        closeIntenet = true;
    }
    if (hour == 21) {

    }
}

function jingdong() {
    try {
        myFunc.openMyApp("京东");
        var a = text("种豆得豆").findOne(5000);
        if (a == null) {
            myFunc.tuichu("京东");
            jingdong();
        } else {
            // diandianquan(1000);
            myFunc.pClick(a);
            zhongdou();
            // openWeixinJingDou();
            myFunc.tuichu("京东");
        }
    } catch (error) {
        log(error);
        myFunc.tuichu("京东");
        jingdong();
    }

}



function zhongdou() {
    myFunc.log("种豆");
    text("收取收获值").findOne(10000);
    sleep(1000);
    //做任务
    renwu();
    sleep(1000);
    //收营养液
    findXn();
    sleep(1000);
    //收其他人
    findOthern(0);
    // back();
    // sleep(1500);
    // scrollUp();
    // sleep(500);
    // findXn();
    // openWeixinJingDou();
}

function renwu() {
    if(storage.contains('种豆任务')){
        return;
    }
    
    log("任务");
    // 点任务
    click(950, 1550);
    myFunc.findOne("做任务 得更多收获值");        
    sleep(1000);
    myFunc.clickByText('去签到', 1000)
    function dotask(text){
        var a = myFunc.find(text);
        if(a.length == 0){
            return;
        }
        let cy = 0;
        let len = a.length;

        while (len > cy) {
            myFunc.clickCenter(a[cy], 1);
            sleep(6000);
            if(myFunc.findOnce("关注频道任务") != null){
                for (let index = 0; index < 3; index++) {
                    text('进入并关注').findOne(5000).click();
                    sleep(2000);
                    back();
                    sleep(2000);
                }
            } else if(myFunc.findOnce("申请使用你的京东账号") != null){
                back();
                sleep(7000);
            } else {
                sleep(4000);
            }
            myFunc.tuichuToStr("做任务 得更多收获值");
            sleep(1000);
            a = myFunc.find(text);
            if(len == a.length){
                cy++;
            }
        }
    }
    dotask('逛一逛');
    dotask('去完成');

    if(myFunc.findOnce('逛一逛') == null && myFunc.findOnce('去完成') == null && myFunc.findOnce("做任务 得更多收获值") != null){
        storage.put('种豆任务', '');
    }
    //关闭任务
    click(994, 550);
    sleep(1000);
}


function qiandao() {
    if (myFunc.findOne('继续签到', 2000, true) != null) {
        click(160, 200)
    } else {
        var qian = myFunc.clickByTextC("领点点券", 2000);
        if (qian) {
            sleep(1500);
            // if(myFunc.clickByTextC("签到") != null) {
            //     sleep(1500);
            //     myFunc.clickByTextC("签到", 2000);
            // }
            // myFunc.textCpClick("点点券");
            if (myFunc.clickText("更多福利", 1000, true)) {
                return;
            }
            // if (myFunc.clickText("红包", 2000, true)) {
            //     return;
            // }

        } else {
            click(160, 200)
        }
    }
}

function diandianquan(n) {
    myFunc.log("点点券");
    if (storage.contains('点点券')) {
        return;
    }
    var a = text("领券").findOne(5000);
    if (a == null) {
        myFunc.tuichu("京东");
        jingdong();
    } else {
        myFunc.pClick(a);
        sleep(n);
        if (n == 2000) {
            click(989 * widthParam, 3013 * heightParam);
            sleep(1000);
            back();
            sleep(16000);
        }
        qiandao();
        sleep(1500);
        var lin = textStartsWith("待领取").findOne(1000);
        if (lin != null) {
            myFunc.clickCenter(lin);
        }
        sleep(700);
        dianRenwu("领取任务");
        dianRenwu("继续完成");
        var quan = textStartsWith("+").find();
        if (quan != null) {
            quan.forEach(a => {
                myFunc.log(a.text())
                sleep(500);
                myFunc.clickCenter(a);
            })
        }
        if (quan.length < 6 &&
            myFunc.findOnce('领取任务') == null &&
            myFunc.findOnce('点点券', 1) != null &&
            myFunc.findOnce('领取提醒') != null &&
            myFunc.findOnce('继续完成') == null) {
            storage.put('点点券', '');
        }
        myFunc.tuichuToStr('秒杀');
        sleep(1000);
    }
}

function dianRenwu(str) {
    var task = textContains(str).find();
    task.forEach(element => {
        sleep(1000);
        element.click();
        var a = textStartsWith("去完成任务").findOne(1000);
        if (a != null) {
            a.click();
            diandianquan(2000)
        } else if (textContains("/").exists()) {
            var but = textMatches(".*\\d{1,2}/\\d{1,2}.*").findOnce();
            var ty = but.bounds().bottom + 100;
            var taskText = but.text();
            var numIdx = taskText.match('\\d{1,2}/\\d{1,2}')[0].split("/");
            var sleepTime = 1000;
            if (taskText.indexOf('s') != -1) {
                var sPosition = taskText.match('\\d{1,2}s')[0];
                sleepTime = sPosition.substring(0, sPosition.length - 1) * 1000;
            }
            myFunc.log(numIdx, sleepTime);
            for (let y = numIdx[0]; y < numIdx[1]; y++) {
                click(770 * widthParam, ty)
                sleep(sleepTime);
                var word = '天天点点券';
                myFunc.tuichuToStr(word);
                sleep(2000);
            }
            var x = className("android.view.View").indexInParent(but.indexInParent()).depth(but.depth()).clickable().findOnce();
            if (x != null) {
                x.click();
            } else {
                click(1305 * widthParam, 667 * heightParam);
            }
        } else if (text("折学系").findOne(1000) != null) {
            sleep(12000);
            back();
            sleep(1000);
        } else {
            back();
        }
    });
    sleep(1000);
}

function findXn() {
    let b = textStartsWith("+1").find();
    while(b.length > 0){
        for(let i = 0; i < b.length; i++){
            myFunc.clickCenter(b[i])
            sleep(500)
            if(myFunc.findOnce('开心收下') != null){
                myFunc.clickByText('开心收下')
            }
        }
        sleep(1000)
        b = textStartsWith("+1").find();
    }
}

function findOthern(n) {
    var dou = myFunc.findOnce(' 3 ', 1)
    var num = dou.text().split(' 3 ').length -1;
    for (let index = 0; index < num; index++) {
        click(110 + index * 200, 1920)
        myFunc.findOne('你收取TA');
        sleep(500)
        myFunc.clickByText('+3')
        sleep(1000)
    }
}


function findOthern2(n) {
    sleep(1500);
    var num = 3;
    var dou = text(num).find();

    // var dou = textStartsWith("可能认识的人").find();
    if (dou == null || dou.length > 0) {
        // num = num - 1;
        dou = text(num).find();
        if (dou == null || dou.length == 0) {
            return;
        }

    }
    for (let index = 0; index < dou.length; index++) {
        let element = dou[index]
        if (element.bounds().left > 0) {
            myFunc.clickCenter(element);
        }
    }

    sleep(1500);
    var x = className("android.widget.TextView").textStartsWith("x" + num).findOnce();
    if (x != null) {
        myFunc.clickCenter(x);
        if (textContains("上限").findOne(1000) != null) {
            return;
        }
        findOthern2(n + 1);
    };
}

function openWeixinJingDou() {
    if (hour != 11) {
        // return;
    }
    myFunc.openMyApp("微信");
    if (textContains("通讯录").findOne(1000) == null) {
        // myFunc.log("打开两个微信");
        // var a = textContains("微信").find()[0];
        // myFunc.clickCenter(a);
        // sleep(1000);
        // if (textContains("通讯录").findOne(1000) == null) {
        var mima = textContains("密码").findOne(1000);
        if (mima != null) {
            mima.click();
            // textContains("密码").findOnce().click();
            // sleep(1000);
            textContains("输入隐私密码").findOne(1000);
            click(243, 1900);
            sleep(300);
            click(544, 1660);
            sleep(300);
            click(243, 1485);
            sleep(300);
            click(744, 1660);
            sleep(300);
            click(544, 1660);
            sleep(300);
            click(243, 1660);
        }
        // }
    }
    sleep(1000);
    // swipe(200, 500, 200, 2500, 500);
    // sleep(500);
    // swipe(200, 500, 200, 2500, 500);
    // myFunc.textpClick("京东购物", 5000);
    // sleep(3000);
    // var a = myFunc.findOne('领京豆', 7000);
    // if (a != null) {
    //     myFunc.clickCenter(a);
    // } else {
    //     click(500, 830);
    // }

    // sleep(6000);
    // var b = myFunc.findOnce("签到领京豆");
    // if (b != null) {
    //     myFunc.clickCenter(b);
    // } else {
    //     click(540, 567)
    // }
    // sleep(1000);
    // myFunc.tuichuToStr('通讯录')
    log('粉丝福利')
    myFunc.clickByText('心之所向')
    myFunc.clickByText('摊上好事了，手慢无>>')
    sleep(2000);
    if (myFunc.findOne('我的金币资产') != null) {
        myFunc.clickByText('sign-button');
        sleep(1000);
        while (myFunc.findOnce('赚更多金币') != null) {
            if (myFunc.findOnce('已领取') == null && myFunc.findOnce('去完成') != null) {
                while (myFunc.clickTextByBtn('去完成')) {
                    sleep(13000);
                    myFunc.tuichuToStr('赚更多金币')
                }
            } else if (myFunc.findOnce('已领取') != null) {
                break
            } else {
                click(997, 2088)
                sleep(1000);
                myFunc.clickByText('sign-button');
                sleep(2000);
            }
        }
    }

    myFunc.tuichu("微信");
    // var a = text("领京豆").findOne(5000);
    // if (a == null) {
    //     myFunc.tuichu("微信");
    //     openWeixin();
    // } else {
    //     myFunc.clickCenter(a);
    //     sleep(1000);
    //     var b = text("签到领京豆").findOne(5000);
    //     myFunc.log("签到领取京豆");
    //     myFunc.clickCenter(b);
    //     sleep(1000);
    //     myFunc.tuichu("微信");
    // }

}












